﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace Metier
{
    public class GestionZone
    {
        public void Ajouter(Zone zone)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Zones.AddObject(zone);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public Zone Chercher(int zoneId)
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Zones.Where(z => z.Id == zoneId).FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Zone Chercher(Serveur serveur)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from z in db.Zones
                            where z.Serveur.Id == serveur.Id
                            select z).FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Zone> Lister()
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Zones.ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Zone> Lister(Restaurant restaurant)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from z in db.Zones.
                                Include("Table").
                                Include("Serveur")
                            where z.Restaurant.Id == restaurant.Id
                            select z).ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void MettreAJour(Zone zone)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Zones.Attach(zone);
                    db.ObjectStateManager.ChangeObjectState(zone, System.Data.EntityState.Modified);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Supprimer(Zone zone)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Zones.DeleteObject(zone);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
